package view_manage

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
	"vrcm/src/vrcm/app/admin/models/view"
	"vrcm/src/vrcm/app/admin/service/dto/view_manage"
	"vrcm/src/vrcm/app/admin/service/view_manage"
	"vrcm/src/vrcm/common/apis"

	"vrcm/src/vrcm/common/actions"
)

type ViewLimit struct {
	api.Api
}

// GetPage 获取ViewLimit列表
// @Summary 获取ViewLimit列表
// @Description 获取ViewLimit列表
// @Tags ViewLimit
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.ViewLimit}} "{"code": 200, "data": [...]}"
// @Router /api/v1/view-limit [get]
// @Security Bearer
func (e ViewLimit) GetPage(c *gin.Context) {
	req := view_manage.ViewLimitGetPageReq{}
	s := service.ViewLimit{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	p := actions.GetPermissionFromContext(c)
	list := make([]view.ViewLimit, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取ViewLimit失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取ViewLimit
// @Summary 获取ViewLimit
// @Description 获取ViewLimit
// @Tags ViewLimit
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.ViewLimit} "{"code": 200, "data": [...]}"
// @Router /api/v1/view-limit/{id} [get]
// @Security Bearer
func (e ViewLimit) Get(c *gin.Context) {
	req := view_manage.ViewLimitGetReq{}
	s := service.ViewLimit{}

	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object view.ViewLimit

	err = s.Get(&req, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取ViewLimit失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

// GetByBindId 获取页面限制详情
func (e ViewLimit) GetByBindId(c *gin.Context) {
	req := view_manage.ViewLimitGetReq{}
	s := service.ViewLimit{}
	v := service.ViewCheck{}
	var object view.ViewLimitWithCheck
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		MakeService(&v.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	var limit view.ViewLimit
	s.Get(&req, &limit)

	if limit.Id <= 0 {
		e.OK(nil, "查询成功")
		return
	}
	checks := make([]view.ViewCheck, 0, 10)
	re := view_manage.ViewCheckGetReq{ViewId: limit.Id}
	v.Get(&re, &checks)

	whites := make([]view.WhiteUser, 0, 10)
	req1 := view_manage.GetWhiteUserByLimitReq{LimitId: limit.Id}
	s.GetWhiteUserByLimit(&req1, &whites)

	object.WhiteUsers = whites
	object.ViewCheck = checks
	object.ViewLimit = limit

	e.OK(object, "查询成功")
}

// GetByFilter 根据条件获取页面限制详情
func (e ViewLimit) GetByFilter(c *gin.Context) {
	req := view_manage.ViewLimitGetFilterReq{}
	s := service.ViewLimit{}
	v := service.ViewCheck{}
	var object view.ViewLimitWithCheck
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		MakeService(&v.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	var limit view.ViewLimit
	s.GetByFilter(&req, &limit)

	if limit.Id <= 0 {
		e.OK(nil, "查询成功")
		return
	}
	checks := make([]view.ViewCheck, 0, 10)
	re := view_manage.ViewCheckGetReq{ViewId: limit.Id}
	v.Get(&re, &checks)

	object.ViewCheck = checks
	object.ViewLimit = limit

	e.OK(object, "查询成功")
}

// Insert 创建ViewLimit
// @Summary 创建ViewLimit
// @Description 创建ViewLimit
// @Tags ViewLimit
// @Accept application/json
// @Product application/json
// @Param data body dto.ViewLimitInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/view-limit [post]
// @Security Bearer
func (e ViewLimit) Insert(c *gin.Context) {
	req := view_manage.ViewLimitInsertReq{}
	s := service.ViewLimit{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	err = s.Insert(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("创建ViewLimit失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改ViewLimit
// @Summary 修改ViewLimit
// @Description 修改ViewLimit
// @Tags ViewLimit
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.ViewLimitUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/view-limit/{id} [put]
// @Security Bearer
func (e ViewLimit) Update(c *gin.Context) {
	req := view_manage.ViewLimitUpdateReq{}
	s := service.ViewLimit{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	err = s.Update(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改ViewLimit失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除ViewLimit
// @Summary 删除ViewLimit
// @Description 删除ViewLimit
// @Tags ViewLimit
// @Param data body dto.ViewLimitDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/view-limit [delete]
// @Security Bearer
func (e ViewLimit) Delete(c *gin.Context) {
	s := service.ViewLimit{}
	req := view_manage.ViewLimitDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	// req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Remove(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除ViewLimit失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

// GenerateViewCode 生成观看码
func (e ViewLimit) GenerateViewCode(c *gin.Context) {
	s := service.ViewLimit{}
	req := view_manage.GenerateViewCodeReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data := s.GenerateViewCode(&req)
	if err != nil {
		e.Error(apis.GenerateViewCodeError.Code, err, fmt.Sprintf("GenerateViewCodeError失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "重制观看码成功")
}

func (e ViewLimit) SaveWhiteUser(c *gin.Context) {
	s := service.ViewLimit{}
	req := view_manage.SaveWhiteUserReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data := s.SaveWhiteUser(&req)
	if err != nil {
		e.Error(apis.GenerateViewCodeError.Code, err, fmt.Sprintf("保存白名单失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "保存白名单成功")
}

func (e ViewLimit) DeleteWhiteUser(c *gin.Context) {
	s := service.ViewLimit{}
	req := view_manage.DeleteWhiteUserReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data := s.DeleteWhiteUser(&req)
	if err != nil {
		e.Error(apis.GenerateViewCodeError.Code, err, fmt.Sprintf("删除白名单失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "删除白名单成功")
}

func (e ViewLimit) GetWhiteUser(c *gin.Context) {
	s := service.ViewLimit{}
	req := view_manage.GetWhiteUserReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data, err := s.GetWhiteUser(&req)
	if err != nil {
		e.Error(apis.GenerateViewCodeError.Code, err, fmt.Sprintf("获取白名单失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "获取白名单成功")
}

func (e ViewLimit) CheckWhiteUser(c *gin.Context) {
	s := service.ViewLimit{}
	req := view_manage.CheckWhiteUserReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data, err := s.CheckWhiteUser(&req)
	if err != nil {
		e.Error(apis.GenerateViewCodeError.Code, err, fmt.Sprintf("校验白名单失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "校验名单成功")
}
